// Une classe pour lire les fichier DEM (elevation)

package pluginUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Locale;
import java.util.Scanner;


public class DEMReader {
	
	private int maxElevationInt,minElevationInt; 
	private int minTranche[],maxTranche[],valeurs[][]; 
	private int DEMLevelCode;
	private int PatternCode;
	private int PlanimetricReference;
	private int ZoneCode;
	private int columns; 
	
	public int[][] getValeurs(){return valeurs;}
	
	public int[] getMinTranche() {
		return minTranche;
	}

	public int[] getMaxTranche() {
		return maxTranche;
	}

	public int getDEMLevelCode() {
		return DEMLevelCode;
	}

	public int getPatternCode() {
		return PatternCode;
	}

	public int getPlanimetricReference() {
		return PlanimetricReference;
	}

	public int getZoneCode() {
		return ZoneCode;
	}

	public int getColumns() {
		return columns;
	}

	public int getMinTranche(int i) {
		return minTranche[i];
	}

	public int getMaxTranche(int i) {
		return maxTranche[i];
	}

	public int[] getSlice(int k){return valeurs[k]; }	
	
	
	public  void afficheFichierTexte(String nomFichierSource) {
		PrintStream output; 
	   String catena=nomFichierSource; 
      File source = new File(catena);
      int planeUnitCode;
      int elevationUnitCode; 
      int numberOfSides; 
      String minElevation,maxElevation; 
      String angle; 
      
      
      char buffer[]=new char[145]; 
      try {
              BufferedReader in = new BufferedReader(new FileReader(source));
            	  in.read(buffer,0,145);
            	
             
              System.out.println("**"+String.copyValueOf(buffer)+"**"); 
             
             Scanner r=new Scanner(in); 
             r.useLocale(Locale.US); 
             DEMLevelCode=r.nextInt(); 
             PatternCode=r.nextInt(); 
             PlanimetricReference=r.nextInt(); 
             ZoneCode=r.nextInt(); 
             System.out.println("DEM Level Code :"+DEMLevelCode+" Pattern Code :"+PatternCode+" Planimetric Reference :"+PlanimetricReference+" Zone Code :"+ZoneCode); 
             for(int i=0;i<15;i++){
                 String n1=r.next(); 
                 System.out.println("-->"+n1+"<---"); 
                 }
             planeUnitCode=r.nextInt(); 
             System.out.println("Plane unit code : "+planeUnitCode);
             elevationUnitCode=r.nextInt(); 
             System.out.println("Elevation unit code : "+elevationUnitCode);
             numberOfSides=r.nextInt();
             System.out.println("Number of Sides : "+numberOfSides);
             // Corners coordinates
             for(int i=0;i<8;i++)
            	 System.out.println("-->"+r.next()+"<--"); 
             minElevation=r.next(); 
             int posD=minElevation.indexOf("D+"); 
             int exposant=Integer.parseInt(minElevation.substring(posD+2)); 
             minElevationInt=Integer.parseInt(minElevation.substring(2,exposant+2)); 
             maxElevation=r.next(); 
             posD=maxElevation.indexOf("D+"); 
             exposant=Integer.parseInt(maxElevation.substring(posD+2)); 
             maxElevationInt=Integer.parseInt(maxElevation.substring(2,exposant+2)); 
             System.out.println("Min elevation "+minElevationInt+" max Elevation "+maxElevationInt);
             angle=r.next(); 
             System.out.println("Angle "+angle);
             
             // Un entier et 3 flottants colles ensemble
             String pass=r.next(); 
             System.out.println(pass); 
             
             int row=r.nextInt(); 
             columns=r.nextInt(); 
             System.out.println("Rows : "+row+" Columns : " +columns); 
             // fin de la lecture de la partie A
            
             
             valeurs=new int[columns][columns];
             minTranche=new int[columns]; 
              maxTranche=new int[columns]; 
             
              // lecture des lignes B
              
            for(int k=0;k<columns;k++){
             int rowIdent=r.nextInt(); 
             int colIdent=r.nextInt(); 
            
             System.out.println(rowIdent+" "+colIdent); 
             
             int nrow=r.nextInt(); 
             int ncol=r.nextInt(); 
             System.out.println(nrow+" "+ncol); 
             
           
             String coordA=r.next(); 
             String coordB=r.next(); 
             String elevLocalDatum=r.next(); 
             //System.out.println(coordA+" "+coordB+" "+elevLocalDatum);
            
             String s1=r.next(); 
             posD=s1.indexOf("D+"); 
             exposant=Integer.parseInt(s1.substring(posD+2)); 
             minTranche[k]=Integer.parseInt(s1.substring(2,exposant+2)); 
             s1=r.next(); 
             posD=s1.indexOf("D+"); 
             exposant=Integer.parseInt(s1.substring(posD+2)); 
             maxTranche[k]=Integer.parseInt(s1.substring(2,exposant+2)); 
             System.out.println(" *** "+minTranche[k]+" "+maxTranche[k]); 
 
             for(int i=0;i<columns;i++){
            	 valeurs[i][k]=r.nextInt();
             }
             }
            
        
           
      }
      catch(Exception e){System.out.println("Probleme "+e); System.exit(0); }
    
      }
	
	public int getMaxElevationInt() {
		return maxElevationInt;
	}

	public int getMinElevationInt() {
		return minElevationInt;
	}

	public static void main(String[] args) {
		//afficheFichierTexte("C:/Users/decomite/Downloads/032g/032g_0100_deme.dem");
		//afficheFichierTexte("C:/Users/decomite/Downloads/083c07/083c07_0101_deme"); 
		DEMReader toto=new DEMReader(); 
		toto.afficheFichierTexte("C:/Users/decomite/Downloads/grand_canyon-e/grand_canyon-e");
		
	}
	

}
